﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MHPL_DoTrangSuc.Data;

namespace MHPL_DoTrangSuc.ManHinh
{
    public partial class frmThem_HoaDon : Form
    {
        KetNoi kn = new KetNoi();
        public frmThem_HoaDon()
        {
            InitializeComponent();
        }
        //kn.loadcombobox(cbMaDonHang, string strselect, byte chiso);
        private void frmThem_HoaDon_Load(object sender, EventArgs e)
        {
            kn.ketnoi();
            hienthi();
            string query= "select MaDonHang from DonHang WHERE TrangThai='false'";
            
            string q2 = "select TenDNQT from QuanTri";
           
            kn.loadcombobox(cbMaDonHang, query, 0);
            kn.loadcombobox(cbMaNhanVien, q2, 0);
            
            
           
        }
        private void hienthi()
        {

            string str = "SELECT MaHoaDon,NgayLapHoaDon,SoTienThanhToan,MaDonHang,TenQT FROM HoaDon hd, QuanTri qt where hd.TenDNQT=qt.TenDNQT ";
            DataTable tb = new DataTable();
            tb.Load(kn.ThucHienReader(str));
            Load_ListView(listViewHD, tb);


        }
        public void Load_ListView(ListView lvwName, DataTable tb)
        {
            listViewHD.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                string[] col = new string[tb.Columns.Count];
                for (int j = 0; j < tb.Columns.Count; j++)
                {
                    col[j] = tb.Rows[i][j].ToString();
                }
                ListViewItem lvItem = new ListViewItem(col);
                lvwName.Items.Add(lvItem);
            }
        }


        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            if (txtMaHoaDon.Text == "" || cbMaDonHang.Text == "" || cbMaNhanVien.Text == "" || dateTimePicker1.Value.ToString() == "")
            {
                MessageBox.Show("Bạn chưa nhập đủ dữ liệu");
            } 
            else
                
            {
                if (!kn.kttrungkhoa(txtMaHoaDon.Text, "select MaHoaDon from HoaDon"))
                    {
                         string insert = "insert into HoaDon values('" + txtMaHoaDon.Text + "','" +dateTimePicker1.Value.ToString("MM/dd/yyyy") + "',N'" + txtTongTien.Text + "',N'" + cbMaDonHang.Text + "',N'" + cbMaNhanVien.Text + "')";
                        kn.thucthiketnoi(insert);
                        MessageBox.Show("Hóa đơn mới đã được thêm vào");
                        frmThem_HoaDon_Load(sender, e);                              
                    }
                    else
                    {
                         MessageBox.Show("Mã hóa đơn này đã tồn tại, xin vui lòng chọn mã hóa đơn khác", "Trùng tên mã hóa đơn", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                               
                    }
                }
                
            }            

                            
                     
            
        

                    
                        
                    

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            foreach (Control ctr in this.groupThongTinHD.Controls)
            {
                if ((ctr is TextBox) || (ctr is DateTimePicker) || ( ctr is ComboBox))
                {
                    ctr.Text = "";
                }
        }
               
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void cbMaNhanVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            kn.loadtextbox(txtTenQT, "select TenQT from QuanTri where TenDNQT='" + cbMaNhanVien.Text + "'");
            
        }

        private void cbMaDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
           // kn.loadtextbox(txtNgayDatHang, "select NgayDatHang FROM DonHang  WHERE MaDonHang='" + cbMaDonHang.Text + "'");
            kn.loadtextbox(txtTongTien, "select TongTien FROM DonHang  WHERE MaDonHang='" + cbMaDonHang.Text + "'");


        }



    }
}

        

    

